package bm.com.project.server.common.service.impl;

import bm.com.framework.common.config.EnumCode;
import bm.com.project.server.common.dto.BmSysChangeOrgListDTO;
import bm.com.project.server.common.dto.VerifyCodeDTO;
import bm.com.project.server.common.service.CommonService;
import bm.com.project.server.organization.service.OrgService;
import bm.com.project.server.sys.entity.BmSysUserEntity;
import bm.com.project.server.sys.mapper.BmSysUserMapper;
import bm.com.framework.redis.unit.RedisUnits;
import bm.com.framework.common.unit.Tool;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    BmSysUserMapper bmSysUserDao;

    @Resource
    OrgService orgService;

    @Resource
    RedisUnits redisUnits;

    @Override
    public String getVerifyCode(VerifyCodeDTO verifyCodeDTO) {
        String roundString = Tool.getRoundString(EnumCode.NUMBER_THREE, 6, true);
        /** 保存key信息到缓存中 */
        try {
            redisUnits.set(verifyCodeDTO.getUuid(),roundString,EnumCode.TIME_120);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return roundString;
    }


    @Override
    public List<BmSysChangeOrgListDTO> getOrgListByMobile(String mobile) {

        BmSysUserEntity userByMobile = bmSysUserDao.getUserByMobile(mobile);
        if (Tool.isObjectEmpty(userByMobile)){
            return null;
        }

        return orgService.getOrgListCommon(userByMobile.getId());
    }
}
